<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta content="phalconplus API doc" name="description">
    <title>
                    phalconplus API Documentation
            </title>
    <link rel="stylesheet" type="text/css" href="../../../asset/static/prettify.css" />

<script src="../../../asset/static/prettify.js"></script>
<script src="../../../asset/api_definition.js"></script>


                        <link rel="stylesheet" type="text/css" href="../../../asset/static/prettify.css" />
                    <link rel="stylesheet" type="text/css" href="../../../asset/static/zephir-theme.css" />
            
    

                        <script src="../../../asset/static/prettify.js"></script>
                                    <script src="../../../asset/api_definition.js"></script>
                    <script src="../../../asset/static/jquery.min.js"></script>
                    <script src="../../../asset/static/zephir-doc.js"></script>
                <script type="application/javascript">let ZepCurrentPath = '../../../'</script>
    </head>
 <body onload="prettyPrint()">
        <div id="top-bar">
            <div id="top-left">
                                    phalconplus Documentation <span class="version">v1.3.0-development</span>
                            </div>
            <div id="top-right">
            </div>
        </div>
        <div id="body">
            <div id="body-left">
                <div class="header-search">
                    <input class="search-box" placeholder="Search for a class or namespace " type="text" />
                    <div id="menu-wrapper">
                    </div>
                </div>
            </div>
            <div id="body-right">
                <div class="content">
                    
<div class="breadcrumb">
    
    <ul>
        
        <li><a href="../../../index.html">Home</a></li>
        
                <li><a href="../../../classes.html">Classes</a></li>
                <li><a href="../../../class/PhalconPlus/Base/Pagable.html">PhalconPlus\Base\Pagable</a></li>
                <li><a >Source</a></li>
                
    </ul>
    
</div>
<div class="class-header source clearfix">
    
    <div class="access-buttons">
        <a class="zep-button" href="../../../class/PhalconPlus/Base/Pagable.html">Class</a>

            </div>
    
    <div class="class-full-name">
        <span class="class-type">Class</span>
        
                <span class="namespace-piece">
            <a href="../../../namespace/PhalconPlus.html">PhalconPlus</a>
        </span>
                <span class="namespace-piece">
            <a href="../../../namespace/PhalconPlus/Base.html">Base</a>
        </span>
        
        <h1 class="class-name">Pagable</h1>
    </div>
    
</div>

<pre class="zephir-source-file prettyprint linenums">
    namespace PhalconPlus\Base;
use PhalconPlus\Enum\OrderByDirection;
use PhalconPlus\Base\ProtoOrderBy;

class Pagable extends ProtoBuffer
{
    const DEFAULT_PAGE_NO = 1;
    const DEFAULT_PAGE_SIZE = 20;

    protected pageNo = 0;
    protected pageSize = 0;
    protected orderBys = [];

    public function __construct(int pageNo = 0, int pageSize = 0, array orderBys = [])
    {
        if pageNo < 1 {
            let pageNo = Pagable::DEFAULT_PAGE_NO;
        }
        if pageSize < 1 {
            let pageSize = Pagable::DEFAULT_PAGE_SIZE;
        }
        
        this->setPageNo(pageNo)
            ->setPageSize(pageSize);

        if !empty orderBys {
            this->setOrderBys(orderBys);
        }
    }

    public static function fromArray(array pages, bool cursor = false) -> <Pagable>
    {
        int pageNo, pageSize;
        if cursor == false {
            let pageNo = isset(pages["pageNo"]) ? (int) pages["pageNo"] : 0;
            let pageSize = isset(pages["pageSize"]) ? (int) pages["pageSize"] : 0;
        } else {
            let pageSize = (int) pages["limit"];
            let pageNo = intval(ceil(intval(pages["offset"]) / pageSize)) + 1; 
        }
        var clsInst = null,
            clsName = get_called_class();
        let clsInst = new {clsName}(pageNo, pageSize);
        if isset pages["orderBys"] {
            clsInst->setOrderBys(pages["orderBys"]);
        }
        return clsInst;
    }

    public function getPageNo() -> int
    {
        return this->pageNo;
    }

    public function getPageSize() -> int
    {
        return this->pageSize;
    }

    public function getOrderBys() -> <ProtoOrderBy>
    {
        return this->orderBys;
    }

    public function getOffset() -> int
    {
        return (this->pageNo - 1) * this->pageSize;
    }

    public function getLimit() -> int
    {
        return this->pageSize;
    }

    public function setOrderBy(<ProtoOrderBy> orderBy) -> <Pagable>
    {
        array_push(this->orderBys, orderBy);
        return this;
    }

    public function setOrderBys(array orderBys) -> <Pagable>
    {
        var item;
        for item in orderBys {
            if isset(item["property"]) && isset(item["direction"]) {
                var orderBy, direction,
                    upperDirection = strtoupper(item["direction"]);

                let direction = new OrderByDirection(upperDirection);
                let orderBy = new ProtoOrderBy();
                orderBy->setProperty(item["property"])
                       ->setDirection(direction);
                this->setOrderBy(orderBy);
            }
        }
        return this;
    }

    public function hasOrderBy() -> boolean
    {
        return ! empty(this->orderBys);
    }

    public function setPageNo(var pageNo) -> <Pagable>
    {
        let this->pageNo = pageNo;
        return this;
    }

    public function setPageSize(var pageSize) -> <Pagable>
    {
        let this->pageSize = pageSize;
        return this;
    }

}
</pre>
                </div>
            </div>
        </div>
    </body>
</html>
